package com.trkj.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.trkj.dao.ArchiveDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.QuitDAO;
import com.trkj.pojo.Archive;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Quit;
import com.trkj.vo.HistoryTaskVo;
import com.trkj.vo.QuitInfoVo;

@Service
@Transactional
public class RSquitService {
	@Autowired
	private HistoryService historyService;
	@Autowired
	QuitDAO quitDAO;
	@Autowired
	ArchiveDAO archiveDAO;
	@Autowired
	EmpDAO empDAO;

	/**
	 * 从流程表中遍历获取转正编号ID
	 * 
	 * @author csj
	 * @return
	 */
	public List<HistoryTaskVo> getFinishedQuitAssignee(String processId) {
		HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery()
				.variableValueEquals("processid", Integer.valueOf(processId)).processDefinitionKey("quit_act")
				.singleResult();
		if (hpi != null) {
			List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
					.processInstanceId(hpi.getId()).list();
			List<HistoryTaskVo> htvlist = new ArrayList<>();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
			for (HistoricTaskInstance hti : list) {
				HistoryTaskVo htv = new HistoryTaskVo();
				Emp emp = empDAO.findOne(Integer.valueOf(hti.getAssignee()));
				htv.setTaskName(hti.getName());
				htv.setAssignee(emp.getEmpName());
				System.out.println(hti.getEndTime());
				if (hti.getEndTime() != null) {
					htv.setEndTime(sf.format(hti.getEndTime()));
				}
				htv.setStartTime(sf.format(hti.getStartTime()));
				System.out.println(hti.getEndTime());
				if (hti.getEndTime() != null) {
					htv.setTaskState("完成");
				} else if (hti.getStartTime() != null && hti.getEndTime() == null) {
					System.out.println(hti.getEndTime());
					htv.setTaskState("进行中");
				}
				htvlist.add(htv);
			}
			return htvlist;
		}
		return null;
	}
	/**
	 * 从流程表中遍历获取离职编号ID
	 * 
	 * @author csj
	 * @return
	 */
	public List<String> getFinishedQuitProcessIds() {
		System.out.println("111111111");
		List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().deploymentId("35001")
				.finished().list();
		List<String> processIdList = new ArrayList<>();
		for (HistoricProcessInstance hpi : list) {
			HistoricVariableInstance hvi = historyService.createHistoricVariableInstanceQuery()
					.processInstanceId(hpi.getId()).variableName("processid").singleResult();
			processIdList.add(String.valueOf(hvi.getValue()));
			System.out.println(String.valueOf(hvi.getValue()));
		}
		return processIdList;
	}

	/**
	 * 过滤离职信息的带条件或不带调价的查询
	 * 
	 * @author csj
	 * @param empName
	 * @param deptName
	 * @return
	 */
	public List<QuitInfoVo> findQuitInfo(String empName, String deptName) {
		System.out.println("yyyyyyyyyyyyyyy");
		List<String> quitidlist = getFinishedQuitProcessIds();
		List<QuitInfoVo> list = new ArrayList<>();
		for (String quitid : quitidlist) {
			Quit quit = quitDAO.findOne(Integer.valueOf(quitid));
			QuitInfoVo vo = new QuitInfoVo();
			vo.setEmpId(quit.getEmp().getEmpId() + "");
			vo.setEmpName(quit.getEmp().getEmpName());
			vo.setPositionName(quit.getEmp().getPosition().getPositionName());
			vo.setQuitUptime(quit.getQuitUptime() + "");
			vo.setEmpEntertime(quit.getEmp().getEmpEntertime() + "");
			Archive archive = archiveDAO.findOne(quit.getEmp().getEmpId());
			vo.setEmpPositivetime(archive.getArchivesPositivetime() + "");
			vo.setQuitExplain(quit.getQuitExplain());
			vo.setDeptName(quit.getEmp().getDept().getDeptName());
			vo.setQuitId(quit.getQuitId() + "");
			if (deptName != null && deptName.length() > 0 && empName != null && empName.length() > 0) {
				if (vo.getEmpName().indexOf(empName) >= 0 && vo.getDeptName().indexOf(deptName) >= 0) {
					list.add(vo);
				}
			} else if (deptName != null && deptName.length() > 0) {
				if (vo.getDeptName().indexOf(deptName) >= 0) {
					list.add(vo);
				}
			} else if (empName != null && empName.length() > 0) {
				if (vo.getEmpName().indexOf(empName) >= 0) {
					list.add(vo);
				}
			} else {
				list.add(vo);
			}
		}
		return list;
	}

	/**
	 * 将过滤出的集合进行分页
	 * 
	 * @author csj
	 * @param empName
	 * @param deptName
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> getpageQuit(String empName, String deptName, Integer page, Integer rows) {
		Map<String, Object> map = new HashMap<>();
		if (page != null && page > 0 && rows != null && rows > 0) {
			System.out.println("zzzzzzzzz");
			List<QuitInfoVo> list = findQuitInfo(empName, deptName);
			List<QuitInfoVo> list2 = new ArrayList<>();
			int i = (page - 1) * rows;// 起始下标
			int a = (page - 1) * rows;// 游动下标
			while (a - i < rows) {// 控制循环次数，固定为rows次
				if (a <= list.size() - 1) {// 防止最后一页时数组越界
					list2.add(list.get(a));// 在集合中加入当前页的数据
					a++;// 游动下标自加
				} else {
					break;// 最后一页，下标将越界时，退出循环
				}
			}
			map.put("total", list.size());
			map.put("rows", list2);
		}
		return map;
	}

}
